package com.yanqu.road.dao.impl.activity.yearmaster;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.yearMaster.YearMasterBoss;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class YearMasterBossDaoImpl extends TempDataObjectDao<YearMasterBoss> {
    public static String getYearMasterBossSql = "select * from t_c_activity_year_master_boss where activityId=?";

    public YearMasterBoss getYearMasterBoss(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;

        YearMasterBoss yearMasterBoss = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getYearMasterBossSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                yearMasterBoss = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return yearMasterBoss;
    }

    public static String updateYearMasterBossSql = "update t_c_activity_year_master_boss set currentBlood=? where activityId=?";

    @Override
    protected boolean updateData(YearMasterBoss yearMasterBoss) {
        yearMasterBoss.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, yearMasterBoss.getCurrentBlood()));
        para.put(i++, new DbParameter(Types.INTEGER, yearMasterBoss.getActivityId()));
        if (getDb().executeNoneQuery(updateYearMasterBossSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertYearMasterBossSql = "insert into t_c_activity_year_master_boss(activityId,currentBlood) values (?,?)";

    @Override
    protected boolean addData(YearMasterBoss yearMasterBoss) {
        yearMasterBoss.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, yearMasterBoss.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, yearMasterBoss.getCurrentBlood()));
        if (getDb().executeNoneQuery(insertYearMasterBossSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected YearMasterBoss parseData(ResultSet rs) throws SQLException {
        YearMasterBoss yearMasterBoss = new YearMasterBoss();
        yearMasterBoss.setActivityId(rs.getInt("activityId"));
        yearMasterBoss.setCurrentBlood(rs.getLong("currentBlood"));

        return yearMasterBoss;
    }

}
